/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.AppUserInfo;
import me.zhengjie.modules.anniversary.domain.AssociationUserInfo;
import me.zhengjie.modules.anniversary.domain.SelectUser;
import me.zhengjie.modules.anniversary.repository.AppUserInfoRepository;
import me.zhengjie.modules.anniversary.repository.AssociationUserInfoRepository;
import me.zhengjie.modules.anniversary.repository.SelectUserRepository;
import me.zhengjie.modules.anniversary.service.SelectUserService;
import me.zhengjie.modules.anniversary.service.dto.AppUserInfoQueryCriteria;
import me.zhengjie.modules.anniversary.service.dto.AssociationUserInfoQueryCriteria;
import me.zhengjie.modules.anniversary.service.dto.SelectUserDto;
import me.zhengjie.modules.anniversary.service.dto.SelectUserQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.SelectUserMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class SelectUserServiceImpl implements SelectUserService {

    private final AppUserInfoRepository appUserInfoRepository;
    private final AssociationUserInfoRepository associationUserInfoRepository;
    private final SelectUserRepository selectUserRepository;
    private final SelectUserMapper selectUserMapper;

    @Override
    public Map<String,Object> queryAll(SelectUserQueryCriteria criteria, Pageable pageable){
        if(criteria.getActivitiesId()!=null)
        {
            AppUserInfoQueryCriteria aiqc = new AppUserInfoQueryCriteria();
            aiqc.setActivitiesId(criteria.getActivitiesId());
            List<AppUserInfo> appUserInfoList = appUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, aiqc, criteriaBuilder));
            criteria.setActivitiesId(null);
            if(!appUserInfoList.isEmpty())
            {
                for (AppUserInfo item : appUserInfoList) {
                    criteria.getUserIds().add(item.getUserId());
                }
            }
        }
        else if(criteria.getAssociationId()!=null)
        {
            AssociationUserInfoQueryCriteria auiqc = new AssociationUserInfoQueryCriteria();
            //auiqc.setAssociationId(criteria.getAssociationId());
            List<AssociationUserInfo> associationUserInfoList = associationUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, auiqc, criteriaBuilder));
            criteria.setAssociationId(null);
            if(!associationUserInfoList.isEmpty())
            {
                for (AssociationUserInfo item : associationUserInfoList) {
                    criteria.getUserIds().add(item.getUserId());
                }
            }
        }

        Page<SelectUser> page = selectUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(selectUserMapper::toDto));
    }

    @Override
    public List<SelectUserDto> queryAll(SelectUserQueryCriteria criteria){
        return selectUserMapper.toDto(selectUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public void download(List<SelectUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SelectUserDto selectUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", selectUser.getUserId());
            map.put("用户类型", selectUser.getUserType());
            map.put("名称", selectUser.getName());
            map.put("性别", selectUser.getGender());
            map.put("出生年月", selectUser.getBirthDate());
            map.put("民族ID", selectUser.getNationId());
            map.put("政治面貌ID", selectUser.getPoliticalId());
            map.put("联系电话", selectUser.getPhone());
            map.put("是否参会", selectUser.getJoined());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
